@file:OptIn(ExperimentalMaterial3Api::class)

package com.ljj.bitetime.ui.activity

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.filled.ArrowBack
import androidx.compose.material3.CenterAlignedTopAppBar
import androidx.compose.material3.DividerDefaults
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import com.ljj.bitetime.R
import com.ljj.bitetime.ui.theme.BiteTimeTheme

class AboutActivity : ComponentActivity() {
    private val context = this

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            BiteTimeTheme {
                AboutScreen()
            }
        }
    }

    @Composable
    fun AboutScreen() {
        val appNameZh = stringResource(id = R.string.app_name_zh)
        val appNameEn = stringResource(id = R.string.app_name_en)
        val appVersion = stringResource(id = R.string.app_version)
        val author = stringResource(id = R.string.author)
        val repositoryAddress = stringResource(id = R.string.repository_address)
        val license = stringResource(id = R.string.license)
        Scaffold(
            topBar = {
                CenterAlignedTopAppBar(
                    title = { Text("关于") },
                    navigationIcon = {
                        IconButton(onClick = { context.finish() }) {
                            Icon(
                                imageVector = Icons.AutoMirrored.Filled.ArrowBack,
                                contentDescription = "返回"
                            )
                        }
                    }
                )
            }
        ) { paddingValues ->
            Column(
                modifier = Modifier
                    .fillMaxSize()
                    .padding(paddingValues)
                    .padding(16.dp),
                horizontalAlignment = Alignment.CenterHorizontally
            ) {
                // 应用图标
                Icon(
                    painter = painterResource(id = R.drawable.ic_launcher_foreground),
                    contentDescription = "应用图标",
                    modifier = Modifier
                        .size(180.dp)
                        .padding(16.dp),
                    tint = MaterialTheme.colorScheme.primary
                )
                // 应用名称
                Text(
                    text = "$appNameZh | $appNameEn",
                    style = MaterialTheme.typography.headlineLarge,
                    fontWeight = FontWeight.Bold,
                    modifier = Modifier.padding(bottom = 8.dp)
                )
                // 分隔线
                HorizontalDivider(
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(vertical = 16.dp),
                    thickness = DividerDefaults.Thickness, color = DividerDefaults.color
                )
                InfoRow(label = "作者：    ", value = author)
                InfoRow(label = "版本：    ", value = appVersion)
                InfoRow(label = "项目地址：  ", value = repositoryAddress)
                Spacer(modifier = Modifier.weight(1f))
                // 版权信息
                Text(
                    text = """
                    © 2025 $appNameEn. All rights reserved.
                    Licensed under the $license
                    Powered by Jetpack Compose, Coil
                """.trimIndent(),
                    style = MaterialTheme.typography.bodySmall,
                    color = MaterialTheme.colorScheme.onSurfaceVariant,
                    textAlign = TextAlign.Center
                )
            }
        }
    }

    @Composable
    fun InfoRow(label: String, value: String) {
        Row(
            modifier = Modifier
                .fillMaxWidth()
                .padding(vertical = 8.dp),
            horizontalArrangement = Arrangement.SpaceBetween
        ) {
            Text(
                text = label,
                style = MaterialTheme.typography.titleMedium,
                fontWeight = FontWeight.Medium
            )
            Text(
                text = value,
                style = MaterialTheme.typography.bodyMedium,
                color = MaterialTheme.colorScheme.onSurfaceVariant
            )
        }
    }
}